<template>
  <el-form ref="form" :model="questionChoice" class="demo-form-inline" label-width="80px" size="small">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-form-item label="学科" prop="subjectID">
          <el-select v-model="questionChoice.subjectID" placeholder="请选择" style="width:100%">
            <el-option v-for="item in subjects" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="二级目录" prop="catalogID">
          <el-select v-model="questionChoice.catalogID" placeholder="请选择" style="width:100%">
            <el-option v-for="item in ndcategorys" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="标签" prop="tags">
          <el-select v-model="questionChoice.tags" placeholder="请选择" style="width:100%">
            <el-option v-for="item in tags" :key="item.id" :label="item.tagName" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="questionChoice.keyword" placeholder="根据题干搜索" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="6">
        <el-form-item label="试题类型" prop="questionType">
          <el-select v-model="questionChoice.questionType" placeholder="请选择" style="width:100%">
            <el-option v-for="item in questionTypes" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="难度" prop="difficulty">
          <el-select v-model="questionChoice.difficulty" placeholder="请选择" style="width:100%">
            <el-option label="简单" value="1" />
            <el-option label="一般" value="2" />
            <el-option label="困难" value="3" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="方向" prop="direction">
          <el-select v-model="questionChoice.direction" placeholder="请选择" style="width:100%">
            <el-option v-for="(item,index) in directions" :key="index" :label="item" :value="item" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="录入人" prop="creatorID">
          <el-select v-model="questionChoice.creatorID" placeholder="请选择" style="width:100%">
            <el-option v-for="item in users" :key="item.id" :label="item.username" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :span="6">
        <el-form-item label="题目备注" prop="remarks">
          <el-input v-model="questionChoice.remarks" />
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="企业简称" prop="shortName">
          <el-input v-model="questionChoice.shortName" />
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item label="城市" prop="province">
          <el-select v-model="questionChoice.province" placeholder="请选择" style="width:48%; margin-right: 2%;">
            <el-option v-for="item in provinces" :key="item" :label="item" :value="item" />
          </el-select>
          <el-select v-model="questionChoice.city" placeholder="请选择" style="width:48%; margin-right: 2%;">
            <el-option v-for="item in citys" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="6">
        <el-form-item>
          <el-row type="flex" justify="end">
            <el-button size="small" @click="clear">清除</el-button>
            <el-button type="primary" style="margin-right:0%;" size="small" @click="onSubmit">搜索</el-button>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>

import { getSubject } from '@/api/hmmm/subjects.js'
import { getNdcategory } from '@/api/hmmm/directorys.js'
import { getTags } from '@/api/hmmm/tags.js'
import { direction, questionType } from '@/api/hmmm/constants.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { simple as Usimple } from '@/api/base/users.js'
export default {
  // props: {
  //   formData: {
  //     type: Object,
  //     required: true
  //   }
  // },
  data () {
    return {
      questionChoice: {
        subjectID: undefined, // 学科
        catalogID: undefined, // 二级目录
        tags: undefined, // 标签
        keyword: undefined, // 关键字
        questionType: undefined, // 试题类型
        difficulty: undefined, // 难度
        direction: undefined, // 方向
        creatorID: undefined, // 录入人
        remarks: undefined, // 题目备注
        shortName: undefined, // 企业简称
        province: undefined, // 城市
        city: undefined // 地区
      },
      // 学科简单列表
      subjects: [],
      // 二级目录
      ndcategorys: [],
      // 题型列表
      questionTypes: questionType,
      // 标签
      tags: [],
      // 录入人列表
      users: [],
      // 方向列表
      directions: direction,
      // 城市数据
      provinces: provinces(),
      // 城市下地区
      citys: []

    }
  },
  watch: {
    // 如果被监听的数据是个对象，对象中的属性发生改变时，监听是不会触发上的
    // 单独监听obj中的name属性，只要其修改watch就会触发
    'questionChoice.subjectID': async function (newValue) {
      // 清空学科联动数据
      this.questionChoice.catalogID = undefined
      this.questionChoice.tags = undefined
      // console.log('旧值:', oldValue, '; 新值:', newValue)
      // 调用方法 获取二级目录
      this.ndcategorys = await getNdcategory(newValue)
      // 调用方法 获取标签
      this.tags = await getTags(newValue)
      console.log(this.tags)
    },
    'questionChoice.province': function (newValue) {
      // 清空地区数据
      this.questionChoice.city = ''
      // 获取地区列表
      this.citys = citys(newValue)
    }
  },
  async created () {
    // 获取学科列表
    this.subjects = await getSubject()
    // 调用方法 录入人列表
    this.getCreatorID()
  },
  methods: {
    onSubmit () {
      this.$emit('search', this.questionChoice)
    },
    // 获取录入人列表
    async getCreatorID () {
      const { data: res } = await Usimple()
      // console.log(res)
      this.users = res
    },
    // 清空表单
    clear () {
      console.log('清除')
      this.$refs.form.resetFields()
      this.$emit('clear')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-row {
    &:last-child {
      margin-bottom: 0;
    }
  }
</style>
